package com.搜索和回溯;

// Definition for a Node.
class Node {
    public int val;
    public Node left;
    public Node right;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val,Node _left,Node _right) {
        val = _val;
        left = _left;
        right = _right;
    }
}
class Solution7 {
    //定义前结点和当前结点
    Node pre,head;
    public Node treeToDoublyList(Node root) {
        if(root==null) return null;
        //进行中序遍历
        dfs(root);
        //将链表头尾相连，形成循环链表
        head.left=pre;
        pre.right=head;
        return head;
    }
    void dfs(Node cur){
        if(cur==null) return;
        dfs(cur.left);
        //没有前驱结点则为第一个结点
        if(pre!=null) pre.right=cur;
        else head=cur;
        cur.left=pre;
        pre=cur;
        dfs(cur.right);
    }
}
public class 剑指Offer36二叉搜索树与双向链表 {
    public static void main(String[] args) {

    }
}
